package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.His_Push_Adapter;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.Utils;
import striveen.util.used.view.view.XListView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 15/6/25.
 */
public class HistoryPushActivity extends BaseUIActivity implements AdapterView.OnItemClickListener, XListView.IXListViewListener {

    @ViewInject(id = R.id.history_push)
    private XListView history_push_lv;

    @ViewInject(id = R.id.nodata_layout)
    private LinearLayout nodata_layout;

    private List<JsonMap<String, Object>> data;
    private His_Push_Adapter his_push_adapter;
    private int pageSize = 10;
    private int currentPage = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_push_layout);
        setCenter_title(R.string.msg, true);

        history_push_lv.setPullRefreshEnable(true);
        history_push_lv.setPullLoadEnable(false);
        history_push_lv.setOnItemClickListener(this);
        history_push_lv.setXListViewListener(this);
        getHistoryData();
    }

    private void getHistoryData() {
        loadingToast.show();
        String userId = Utils.getUserId(this);
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("UserId", userId);
        params.put("pageSize", pageSize);
        params.put("currentPage", currentPage + 1);
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_SelectPushMessage);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(GetDataConfing.What_SelectPushMessage);
        getDataUtil.getData(queue);
    }

    private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

        @Override
        public void onLoaded(GetDataQueue entity) {
            if (entity.isOk()) { //网络OK
                if (ShowGetDataError.isOK(HistoryPushActivity.this, entity.getInfo())) { // 服务器返回
                    String info = entity.getInfo();
                    List list = JsonParseHelper.getJsonMap_List_JsonMap(info, JsonKeys.Key_Info);
                    initData(list);
                }
            } else {
                ShowGetDataError.showNetError(HistoryPushActivity.this);
                //finish();
            }
            loadingToast.dismiss();
            stopLoad();
            invalidateDatas();
        }

    };

    private void stopLoad() {
        history_push_lv.stopLoadMore();
        history_push_lv.stopRefresh();
    }

    private void initData(List list) {
        if (currentPage == 0) {
            data = list;
            his_push_adapter = new His_Push_Adapter(this);
            his_push_adapter.setDatas(data);
            history_push_lv.setAdapter(his_push_adapter);
        } else {
            data.addAll(list);
            his_push_adapter.setDatas(data);
            his_push_adapter.notifyDataSetChanged();
        }

        if (list.size() % pageSize != 0 || list.size() == 0) {  //j加载到最后 不需要处理野马
            if (currentPage != 0) {
                toast.showToast(R.string.xlistview_no_more_data);
            }
            history_push_lv.setPullLoadEnable(false);
        } else {
            currentPage++;
            history_push_lv.setPullLoadEnable(true);
        }
    }


    // 数据为空显示提示
    private void invalidateDatas() {
        if (data == null || data.size() == 0) {
            nodata_layout.setVisibility(View.VISIBLE);
            history_push_lv.setVisibility(View.GONE);
        } else {
            nodata_layout.setVisibility(View.GONE);
            history_push_lv.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(position>0) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", data.get(position - 1).toJson());
            Intent intent = new Intent(this, ShowPushInfosActivity.class);
            intent.putExtras(bundle);
            startActivityForResult(intent, position - 1);
        }
        // finish();
    }

    @Override
    public void onRefresh() {
        data.clear();
        history_push_lv.setAdapter(null);
        history_push_lv.setPullLoadEnable(false);
        his_push_adapter.setDatas(null);
        currentPage = 0;
        getHistoryData();
    }

    @Override
    public void onLoadMore() {
        getHistoryData();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        data.get(requestCode).put("IsRead",true);
        his_push_adapter.setDatas(data);
         his_push_adapter.notifyDataSetChanged();
        super.onActivityResult(requestCode, resultCode, intent);
    }
}